package com.aier.school.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.school.entity.DailyFood;

public class DailyFoodRowMapper implements RowMapper<DailyFood> {

    private int type;
    
    public DailyFoodRowMapper() {
    }

    public DailyFoodRowMapper(int type) {
        this.type = type;
    }
    
    @Override
    public DailyFood mapRow(ResultSet rs, int rowNum) throws SQLException {
        // TODO Auto-generated method stub
        return parseDailyFood(new DailyFood(), rs);
    }
    
    public static DailyFood parseDailyFood(DailyFood df, ResultSet rs) throws SQLException {
        return parseDailyFood(df, rs, null);
    }

    public static DailyFood parseDailyFood(DailyFood df, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            df.setDfid(rs.getLong(su+"dfid"));
        } catch (SQLException e) {
            df.setDfid(null);
        }
        try {
            df.setContent(rs.getString(su+"content"));
        } catch (SQLException e) {
            df.setContent(null);
        }
        try {
            df.setSid(rs.getInt(su+"sid"));
        } catch (SQLException e) {
            df.setSid(null);
        }
        try {
            df.setDdate(rs.getString(su+"ddate"));
        } catch (SQLException e) {
            df.setDdate(null);
        }
        try {
            df.setState(rs.getShort(su+"state"));
        } catch (SQLException e) {
            df.setState(null);
        }
        try {
            df.setIsmessage(rs.getShort(su+"ismessage"));
        } catch (SQLException e) {
            df.setIsmessage(null);
        }
        try {
            df.setCdate(rs.getTimestamp(su+"cdate"));
        } catch (SQLException e) {
            df.setCdate(null);
        }
        try {
            df.setImg(rs.getString(su+"img"));
        } catch (SQLException e) {
            df.setImg(null);
        }
        
        return df;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, DailyFood df) {
        par.put("dfid",df.getDfid());
        par.put("content",df.getContent());
        par.put("sid", df.getSid());
        par.put("ddate",df.getDdate());
        par.put("state", df.getState());
        par.put("ismessage", df.getIsmessage());
        par.put("cdate",df.getCdate());
        par.put("img",df.getImg());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args, DailyFood df) {
        args.put("dfid",df.getDfid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final DailyFood df) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(df),
                        new String[] { "fid" });
                parsePS(ps, df);
                return ps;
            }

        };
    }

    private static String parseSQL(DailyFood df) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into dailyfood(");
        int count = 0;
        
        if (null != df.getContent()) {
            sql.append("content,");
            count++;
        }
        
        if (null !=df.getSid()) {
            sql.append("sid,");
            count++;
        }
        
        if (null !=df.getDdate()) {
            sql.append("ddate,");
            count++;
        }
        if (null !=df.getState()) {
            sql.append("state,");
            count++;
        }
        if (null !=df.getIsmessage()) {
            sql.append("ismessage,");
            count++;
        }
        
        if (null !=df.getCdate()) {
            sql.append("cdate,");
            count++;
        }

        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }

    private static void parsePS(PreparedStatement ps, DailyFood df)
        throws SQLException {
        int count = 1;
        if (null != df.getContent()) {
            ps.setString(count++,df.getContent());
        }
        if (null != df.getSid()) {
            ps.setInt(count++, df.getSid());
        }
        if (null != df.getDdate()) {
            ps.setString(count++, df.getDdate());
        }
        if (null != df.getState()) {
            ps.setShort(count++, df.getState());
        }
        if (null != df.getIsmessage()) {
            ps.setShort(count++, df.getIsmessage());
        }
        if (null != df.getCdate()) {
            ps.setTimestamp(count++, df.getCdate());
        }


    }

}
